package com.ftg.learn.chapter10.base02.mapper.sql;

import org.apache.ibatis.annotations.Param;

/**
 * @author 布衣
 */
public class StudentSql {
    public String listSql(@Param("stuName") String stuName, @Param("className") String className) {
        StringBuilder sql = new StringBuilder("select student_id studentId,name ,age,class_name className");
        sql.append(" from students a join classes b on a.class_id = b.class_id where true ");
        if (stuName != null && !"".equals(stuName)) {
            sql.append(" and a.name like ").append("concat(#{stuName,'%'})");
        }
        if (className != null && !"".equals(className)) {
            sql.append(" and b.class_name like ").append("concat(#{className,'%'})");
        }
        return sql.toString();
    }

//    public String listSql(@Param("stuName") String stuName, @Param("className") String className,
//                          @Param("start") Integer start, @Param("pageSize") Integer pageSize) {
//        StringBuilder sql = new StringBuilder("select student_id studentId,name ,age,class_name className");
//        sql.append(" from students a join classes b on a.class_id = b.class_id where true ");
//        if (stuName != null && !"".equals(stuName)) {
//            sql.append(" and a.name like ").append("concat(#{stuName,'%'})");
//        }
//        if (className != null && !"".equals(className)) {
//            sql.append(" and b.class_name like ").append("concat(#{className,'%'})");
//        }
//        start = (start - 1) * pageSize;
//        sql.append(" limit " + start + ",#{pageSize}");
//        return sql.toString();
//    }

    public String countSql(@Param("stuName") String stuName, @Param("className") String className) {
        StringBuilder sql = new StringBuilder("select count(student_id) ");
        sql.append(" from students a join classes b on a.class_id = b.class_id where true ");
        if (stuName != null && !"".equals(stuName)) {
            sql.append(" and a.name like ").append("concat(#{stuName,'%'})");
        }
        if (className != null && !"".equals(className)) {
            sql.append(" and b.class_name like ").append("concat(#{className,'%'})");
        }
        return sql.toString();
    }

}
